package com.feituan.core.persistence.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feituan.core.persistence.mapper.PosterImageMapper;
import com.feituan.core.persistence.mapper.SeqNumMapper;
import com.feituan.core.persistence.service.IPosterImageService;
import com.feituan.core.persistence.service.ISeqNumService;
import com.feituan.core.pojo.entity.PosterImage;
import com.feituan.core.pojo.entity.SeqNum;
import com.feituan.core.pojo.request.CommonPageReq;
import com.feituan.core.pojo.response.page.PageData;
import com.feituan.core.util.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaohu
 * @since 2024-05-09
 */
@Service
public class SeqNumServiceImpl extends ServiceImpl<SeqNumMapper, SeqNum> implements ISeqNumService {

    @Override
    public Integer getAndSetNum(String key,String fieldKey) {
        SeqNum seqNum = this.lambdaQuery().eq(SeqNum::getDicKey,key).one();
        if(seqNum != null){
            SeqNum seqNumUpdate =new SeqNum();
            if(fieldKey.equals(seqNum.getFieldKey())){
                seqNumUpdate.setDicValue(seqNum.getDicValue()+1);
            }else {
                seqNumUpdate.setDicValue(1);
                seqNumUpdate.setFieldKey(fieldKey);
            }

            seqNumUpdate.setDicKey(key);
            seqNumUpdate.setId(seqNum.getId());
            this.updateById(seqNumUpdate);
            return seqNumUpdate.getDicValue();
        }else {
            seqNum =new SeqNum();
            seqNum.setDicKey(key);
            seqNum.setFieldKey(fieldKey);
            seqNum.setDicValue(1);
            seqNum.setCreateTime(Calendar.getInstance().getTime());
            this.save(seqNum);
            return 1;
        }
    }
}
